package cn.cmvideo.springboot.dao.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import cn.cmvideo.springboot.dao.UserDao;
import cn.cmvideo.springboot.entity.User;

@Component
public class UserDaoImpl implements UserDao
{

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void saveUser(User user)
	{
		mongoTemplate.save(user);
	}

	@Override
	public User findUserByUserName(String userName)
	{
		Query query = new Query(Criteria.where("userName").is(userName));
		User user = mongoTemplate.findOne(query, User.class);
		return user;
	}

	@Override
	public void updateUser(User user)
	{
		Query query = new Query(Criteria.where("userId").is(user.getUserId()));
		Update update = new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
		mongoTemplate.updateFirst(query, update, User.class);
//		mongoTemplate.updateMulti(query, update, User.class);
	}

	@Override
	public void deleteUserByUserId(int userId)
	{
		Query query = new Query(Criteria.where("userId").is(userId));
		mongoTemplate.remove(query,User.class);
	}

	@Override
	public void deleteUserByUserName(String userName)
	{
		Query query = new Query(Criteria.where("userName").is(userName));
		mongoTemplate.remove(query,User.class);
	}
}
